package edu.cmu.abi.track;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

import org.apache.commons.lang.ArrayUtils;

public class Attribute{
	private int id; 
	private ArrayList<Segment> segments = new ArrayList<Segment>();
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public ArrayList<Segment> getSegments() {
		return segments;
	}

	public void setSegments(ArrayList<Segment> segments) {
		this.segments = segments;
	}

	public Attribute(int id,double[] column,int[] segmentIntervals){
		this.id=id;
		segment(column,segmentIntervals);
	}
		
	private void segment(double[] column, int[] segmentIntervals){
		int start = 0;
		int end = 0;
		int count=0;
		for(int i:segmentIntervals){
			start=end;
			end=i;
			double[] temp = ArrayUtils.subarray(column, start, end);
			Segment s = new Segment(temp);
			s.setSegment_id(count++);
			segments.add(s);
		}
	}
	public double[] getMeans(){
		ArrayList<Double> means = new ArrayList<Double>();
		Iterator<Segment> it = segments.iterator();
		while(it.hasNext()){
			Segment seg = (Segment)it.next();
			means.add(seg.getMean());
		}
		return ArrayUtils.toPrimitive(means.toArray(new Double[0]));
	}
	public double[] getVariances(){
		ArrayList<Double> variances = new ArrayList<Double>();
		Iterator<Segment> it = segments.iterator();
		while(it.hasNext()){
			Segment seg = (Segment)it.next();
			variances.add(seg.getVariance());
		}
		return ArrayUtils.toPrimitive(variances.toArray(new Double[0]));
	}
	public String toString (){
		DecimalFormat df = new DecimalFormat("##.##");
		StringBuffer sb = new StringBuffer();
		sb.append("\tATTRIBUTE:"+this.id+"\n");
		Iterator<Segment> it = segments.iterator();
		while(it.hasNext()){
			Segment seg = it.next();
			sb.append("\t"+seg.getSegment_id()+":(");
			sb.append(df.format(seg.getMean()));
			sb.append(", ");
			sb.append(df.format(seg.getVariance()));
			sb.append(")\n");
		}
		return sb.toString();
	}
}
